﻿using System;
using System.Collections.Generic;
using System.Web;

namespace Daroctrius.WebTV
{
    public class Items : List<Item>
    {
        new public void Add(Item item)
        {
            item.ContentUrl = FormatUrl(item.ContentUrl);
            item.ThumbnailUrl = FormatUrl(item.ThumbnailUrl);

            base.Add(item);
        }

        private string FormatUrl(string url)
        {
            string baseUrl;

            if (url != null)
            {
                if (url.StartsWith("/"))
                {
                    url = url.Replace("//", "/");

                    baseUrl = String.Format("http://{0}",
                        HttpContext.Current.Request.Url.Host);

                    if (!HttpContext.Current.Request.Url.IsDefaultPort)
                    {
                        baseUrl += String.Format(":{0}",
                            HttpContext.Current.Request.Url.Port);
                    }

                    url = baseUrl + url;
                }
            }

            return url;
        }        
    }
    
    public class Item
    {
        public string ContentUrl = "", Description = "", Name = "", ThumbnailUrl = "";
        public ItemType Type;
    }

    public enum ItemType { Folder, PlayNowVideo, Video }
}